package com.shxt.service;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.shxt.data.D_UserMapper;
import com.shxt.pojo.D_User;

public class D_UserService implements D_UserMapper {
	
	private static D_UserService instance = null;
	
	public static D_UserService getInstance(){
		if(instance==null){
			instance = new D_UserService();
		}
		return instance;
	}
	
	@Override
	public int addUser(D_User user) {
		SqlSession session = MyFactory.sessionFactory().openSession();
		int i = 0;
		try{
			D_UserMapper dum = session.getMapper(D_UserMapper.class);
			dum.addUser(user);
			session.commit();
			i=1;
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return i;
	}

	@Override
	public D_User validateUser(Map<String,String> user) {
		SqlSession session = MyFactory.sessionFactory().openSession();
		D_User d_user = new D_User();
		try{
			D_UserMapper dum = session.getMapper(D_UserMapper.class);
			d_user = dum.validateUser(user);
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return d_user;
	}

	@Override
	public int updateUser(D_User user) {
		SqlSession session = MyFactory.sessionFactory().openSession();
		int i = 0;
		try{
			D_UserMapper dum = session.getMapper(D_UserMapper.class);
			dum.updateUser(user);
			session.commit();
			i=1;
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return i;
	}

	@Override
	public List<String> allUserMail() {
		SqlSession session = MyFactory.sessionFactory().openSession();
		List<String> userMailList = null;
		try{
			D_UserMapper dum = session.getMapper(D_UserMapper.class);
			userMailList=dum.allUserMail();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return userMailList;
	}

}
